package com.gxuwz.eschool;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gxuwz.eschool.SQL.MyDatabaseHelper;

public class LoginActivity extends AppCompatActivity {

    private EditText etUserid;
    private EditText etPassword;
    private Button btnLogin;

    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // 初始化视图
        etUserid = findViewById(R.id.user);
        etPassword = findViewById(R.id.password);
        btnLogin = findViewById(R.id.btn_login);

        // 创建数据库帮助类实例
        dbHelper = new MyDatabaseHelper(this);

        // 设置登录按钮点击事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入的用户id和密码
                String userid = etUserid.getText().toString();
                String password = etPassword.getText().toString();

                // 调用数据库帮助类的checkLogin方法进行登录验证
                boolean result = dbHelper.checkLogin(userid, password);

                // 根据验证结果显示相应的提示信息
                if (result) {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    // 登录成功后的操作，跳转到主界面
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "账号/手机号或密码不正确", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}